package com.xinfu.newproject.adapter.paycost_adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.PayCostActivity;
import com.xinfu.newproject.activity.PropertyActivity;
import com.xinfu.newproject.bean.paycost.PayCostBean;

import java.util.List;

public class PayCostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    //设置常量

    private List<PayCostBean> payCostBeans;

    //上下文和数据
    Context context;

    //构造方法
    public PayCostAdapter(Context context, List<PayCostBean> payCostBeans) {
        this.context = context;
        this.payCostBeans = payCostBeans;
    }

    //添加数据
    public void addbean(List<PayCostBean> list) {
        payCostBeans = list;
        //更新数据
        notifyDataSetChanged();
    }


    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_paycost, parent, false);
        RecyclerViewHolder threeViewHolder = new RecyclerViewHolder(view);
        return threeViewHolder;

    }

    //填充onCreateViewHolder方法返回的holder中的控件(当一个View需要出现在屏幕上时，该方法会被回调，我们需要再该方法中根据数据来更改视图)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        setFloorOne((RecyclerViewHolder) holder, position);
    }


    private void setFloorOne(RecyclerViewHolder holder, int position) {
        holder.costNameTv.setText(payCostBeans.get(position).getCostName());
        if (payCostBeans.get(position).getCostType() == 1) {
            holder.imageView.setImageResource(R.mipmap.property_pay_cost_icon);
            holder.relativeLayout.setBackgroundResource(R.mipmap.property_pay_cost_bg);
        } else if (payCostBeans.get(position).getCostType() == 2) {
            holder.imageView.setImageResource(R.mipmap.car_pay_cost_icon);
            holder.relativeLayout.setBackgroundResource(R.mipmap.car_pay_cost_bg);
        }

        holder.relativeLayout.setOnClickListener(this);
    }


    @Override
    public int getItemCount() {
        return payCostBeans == null ? 0 : payCostBeans.size();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_paycost_property_rl:
                ((PayCostActivity) context).startIntentActivity(PropertyActivity.class, null);
                break;
        }
    }

    //还需要重写方法
    // 对应布局类型
//    @Override
//    public int getItemViewType(int position) {
//        if (typeList.get(0) == 1) {
//            return TYPE_IMG_RECYCLER_ONE;
//        } /*else if (typeList.get(0) == 2) {
//            return TYPE_IMG_THREE;
//        }*/
//        return 0;
//    }


    //横向的RecyclerView
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public RelativeLayout relativeLayout;
        public TextView costNameTv;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            costNameTv = itemView.findViewById(R.id.item_paycost_property_type);
            imageView = itemView.findViewById(R.id.item_paycost_property_icon);
            relativeLayout = itemView.findViewById(R.id.item_paycost_property_rl);

        }
    }


}

